<?php
defined('BASEPATH') or exit('No direct script access allowed');

/*
* 公共数据层
*/
class MY_Model extends CI_Model
{
    public $table;
    public $primary_key;

    public function __construct($table, $primary_key = 'id')
    {
        parent::__construct();
        $this->table = $this->db->dbprefix($table);
        $this->primary_key = $primary_key;
    }
    /*
    * 跟据主键获取一条记录
    */
    public function get_by_id($id, $select = "*")
    {
        return (array)$this->db->query("select " . $select . " from " . $this->table . " where `" . $this->primary_key . "`='" . $id . "'")->row_array();
    }
    /*
    * 跟据字段获取一条记录
    */
    public function get_by_field($field, $value, $select = "*")
    {
        return (array)$this->db->query("select " . $select . " from " . $this->table . " where `" . $field . "`='" . $value . "'")->row_array();
    }
    /*
    * 跟据条件获取一条记录
    */
    public function get_by_where($where, $select = "*")
    {
        return (array)$this->db->select($select)->where($where)->get($this->table)->row_array();
    }
    /*
    * 列表记录集
    */
    public function result_data($where = '', $order_by = '', $start = 0, $pagesize = 20)
    {
        $sql = "select * from " . $this->table . " " . $where . " " . $order_by . " limit " . $start . "," . $pagesize;
        $result = $this->db->query($sql)->result_array();
        return $result;
    }

    /*
    * 列表记录集bytable
    */
    public function table_result_data($table, $where = '', $order_by = '', $start = 0, $pagesize = 20)
    {
        $sql = "select * from " .  $this->db->dbprefix($table) . " " . $where . " " . $order_by . " limit " . $start . "," . $pagesize;
        $result = $this->db->query($sql)->result_array();
        return $result;
    }


    /*
    * 所有记录
    */
    public function result_all($where = '', $order_by = '', $select = "*")
    {
        $sql = "select " . $select . " from " . $this->table . " " . $where . " " . $order_by;
        $result = $this->db->query($sql)->result_array();
        return $result;
    }
    /*
    * 列表记录数
    */
    public function result_count($where = '')
    {
        $sql = "select count(1) as total from " . $this->table . " " . $where;
        $row = $this->db->query($sql)->row_array();
        return (int)$row['total'];
    }

    /*
    * 列表记录数bytable
    */
    public function table_result_count($table, $where = '')
    {
        $sql = "select count(1) as total from " . $this->db->dbprefix($table)  . " " . $where;
        $row = $this->db->query($sql)->row_array();
        return (int)$row['total'];
    }
    /*
    * 主健为索引结果数据
    */
    public function result_maps($result)
    {
        if (empty($result)) return array();
        $data = array();
        foreach ($result as $k => $val) $data[$val[$this->primary_key]] = $val;
        return $data;
    }
    /*
    * 字段自增
    */
    public function inc_field($id, $field)
    {
        $sql = "update " . $this->table . " set `" . $field . "`=`" . $field . "`+1 where `" . $this->primary_key . "`='" . $id . "'";
        $this->db->query($sql);
        return true;
    }
    /*
    * 添加记录
    */
    public function insert($insert)
    {
        $this->db->insert($this->table, $insert);
        return $this->db->insert_id();
    }
    /*
    * 更新数据
    */
    public function update($id, $update)
    {
        // var_dump($id, $update);
        // exit;
        if ($id < 1) return false;
        $where = array(
            $this->primary_key => $id,
        );
        $this->db->update($this->table, $update, $where);
        return true;
    }
    /*
    * 删除数据
    */
    public function delete($id)
    {
        if ($id < 1) return false;
        $where = array(
            $this->primary_key => $id,
        );
        $this->db->delete($this->table, $where);
        return true;
    }
    public function delete_where($field, $val)
    {
        if (empty($val)) return false;
        $where = array(
            $field => $val,
        );
        $this->db->delete($this->table, $where);
        return true;
    }
    public function delete_wheres($where){
        if(empty($where)) return false;
        $this->db->delete($this->table, $where);
        return true;
    }
    /*
    * 跟据字段获取一条记录,排除指定id
    */
    public function get_by_field_except($field, $value, $id, $select = "*")
    {
        return (array)$this->db->query("select " . $select . " from " . $this->table . " where `" . $field . "`='" . $value . "' and " . $this->primary_key . "!=" . $id)->row_array();
    }

    /*
    * 跟据指定字段排序，获取第一条数据
    */
    public function get_by_order($order = "",$select = "*",$where="")
    {
        return (array)$this->db->query("select " . $select . " from " . $this->table . " " . $where . " ".$order." limit 1")->row_array();
    }

}
